import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class GamePanel extends ImagePanel implements MouseMotionListener{
	
	private static final long serialVersionUID = 1L;
	private BufferedImage imgForeground;
	private BufferedImage imgBackground;	
	private int mx,my;
	private String imagePath;
		
	public GamePanel(int width, int height, int lx, int ly, int level){
		super(width, height, lx,ly);
		
		switch (level){
		
		case 1: imagePath = "src/Car.jpg"; break;
		case 2: imagePath = "src/Car.jpg"; break;
		case 3: imagePath = "src/Car.jpg"; break;
		default: System.out.println("File Not Found");
		}//end switch
		
		//Read image from file. Right to terminal if file not found.
		try {                
			imgBackground = ImageIO.read(new File(imagePath));
	       } catch (IOException e) {
	            System.out.println("File not found");
	            imgBackground = null;
	       } 

		addMouseMotionListener(this); 
	}//end of Constructor

	@Override
	public void paintComponent(Graphics g){
		
		Graphics2D g2d = (Graphics2D)g;
		g2d.drawImage(imgBackground,null,0,0);	
				
		//draw image at a magnified setting.;
        AffineTransform at = AffineTransform.getScaleInstance(1.5, 1.5);
        AffineTransformOp aop =
        new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
        g2d.drawImage(imgForeground, aop, mx, my);
		
		g.setColor(Color.WHITE);
		g.drawRect(mx,my, Math.min(75, (this.getWidth()-75)),Math.min(75, (this.getHeight()-my)));
		
		
	}//End of paint component.


	public void mouseDragged(MouseEvent me) {
		// TODO Auto-generated method stub
		
	}


	public void mouseMoved(MouseEvent me) {
		mx = me.getX();
		my = me.getY();
		getScreenShot (mx, my);
		repaint();
	
		//Graphics g;
		//g = getGraphics ();
		//g.dispose ();
		
	}
	
	
	private void getScreenShot (int x, int y) {		
		imgForeground = imgBackground.getSubimage(x, y, Math.min(50,(imgBackground.getWidth()-x)), Math.min(50,(imgBackground.getHeight()-y)));
		return;
	}
}
